﻿using System;
using System.Windows.Controls;
using System.Windows;
using System.ComponentModel;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace WickedFlame.VecDraw.Base.Controls
{
    public class SelectableButton : Button, INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        static SelectableButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SelectableButton), new FrameworkPropertyMetadata(typeof(SelectableButton)));
        }

        public SelectableButton()
            : base()
        {
        }

        public bool IsSelected
        {
            get
            {
                return (bool)GetValue(IsSelectedProperty);
            }
            set
            {
                SetValue(IsSelectedProperty, value);
                NotifyPropertyChanged("IsSelected");
            }
        }

        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(SelectableButton));

        public string Title
        {
            get
            {
                return (string)GetValue(TitleProperty);
            }
            set
            {
                SetValue(TitleProperty, value);
            }
        }

        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(SelectableButton));

        public object Icon
        {
            get
            {
                return (object)GetValue(IconProperty);
            }
            set
            {
                SetValue(IconProperty, value);
            }
        }

        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register("Icon", typeof(object), typeof(SelectableButton),
            new FrameworkPropertyMetadata(null, new CoerceValueCallback(OnCoerce_Icon)));

        private static object OnCoerce_Icon(DependencyObject o, object value)
        {
            if (value is string)
            {
                Uri iconUri;
                // try to resolve given value as an absolute URI
                if (Uri.TryCreate(value as String, UriKind.RelativeOrAbsolute, out iconUri))
                {
                    ImageSource img = new BitmapImage(iconUri);
                    if (null != img)
                    {
                        //GreyableImage icon = (o as ManagedContent).Icon as GreyableImage;
                        //if (null == icon)
                        //    icon = new GreyableImage();
                        Image icon = (o as SelectableButton).Icon as Image;
                        if (null == icon)
                            icon = new Image();

                        icon.Source = img;
                        icon.Stretch = Stretch.None;
                        icon.SnapsToDevicePixels = true;

                        return icon;
                    }
                }
            }
            return value;
        }

        public bool IsUnselecteable
        {
            get
            {
                return (bool)GetValue(IsUnselecteableProperty);
            }
            set
            {
                SetValue(IsUnselecteableProperty, value);
                NotifyPropertyChanged("IsUnselecteable");
            }
        }

        public static readonly DependencyProperty IsUnselecteableProperty =
            DependencyProperty.Register("IsUnselecteable", typeof(bool), typeof(SelectableButton), new PropertyMetadata(true));

        //public string Title
        //{
        //    get
        //    {
        //        return (string)GetValue(TitleProperty);
        //    }
        //    set
        //    {
        //        SetValue(TitleProperty, value);
        //    }
        //}

        //public static readonly DependencyProperty TitleProperty =
        //    DependencyProperty.Register("Title", typeof(string), typeof(SelectionButton));

        protected override void OnClick()
        {
            if (IsUnselecteable)
            {
                if (!IsSelected)
                    IsSelected = true;
                else
                    IsSelected = false;
            }
            else
                if (!IsSelected)
                    IsSelected = true;

            base.OnClick();
        }
    }
}
